<template>
  <!--created by zyj 2024/10/29 在生产工单 操作列，增加“生产补料”  -->
  <el-dialog :visible.sync="open" width="960px" append-to-body @close="cancel">
    <template v-slot:title>
      <span>新增生产领料单</span>
    </template>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tag size="large" style="font-size: 20px;">基本信息</el-tag>
      <hr />
      <el-row>
        <el-col :span="10">
          <el-form-item label="补料编码" prop="supplyCode">
            <el-input v-model="form.supplyCode" placeholder="请输入补料编码" v-if="optType === 'add'" />
            <span v-if="optType === 'update' || optType === 'check'">{{ form.supplyCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="80">
            <el-switch v-model="autoGenFlag" active-color="#13ce66" active-text="自动生成"
              @change="handleAutoGenChange(autoGenFlag)" v-if="optType !== 'update' && optType !== 'check'"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="生产工单" prop="workorderCode">
        <el-input v-model="form.workorderCode" placeholder="请输入生产工单" style="width:300px" v-if="optType !== 'check'" />

        <span v-if="optType === 'check'">{{ form.workorderCode }}</span>
      </el-form-item>
      <el-form-item label="BOM版本" prop="bomItemCode">
        <el-input v-model="form.bomItemCode" placeholder="请输入BOM版本" style="width:300px" v-if="optType !== 'check'" />

        <span v-if="optType === 'check'">{{ form.workorderCode }}</span>
      </el-form-item>
      <el-form-item label="补料原因" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入补料原因" v-if="optType !== 'check'" style="width:300px" />
        <span v-if="optType === 'check'">{{ form.remark }}</span>
      </el-form-item>

    </el-form>

    <el-tag size="large" style="font-size: 20px;">物料信息</el-tag>
    <hr />
    <supply-detail ref="supplyDetail" v-if="open" @onSelected="onSelected" :optType="optType"
      :parentId="parentId"></supply-detail>

    <div slot="footer" class="dialog-footer" v-if="optType !== 'check'">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>

import {
  addSupplyBatchInfo,
  updateSupplyBatchInfo
} from "@/api/mes/pro/supply";
import { genCode } from "@/api/mes/md/rule";
import SupplyDetail from "../../materialSupply/supplyDetail.vue";
export default {
  name: "AddMaterialSupply",
  components: { SupplyDetail },
  data() {
    return {
      selectedRows: [],
      optType: "add",
      open: false,
      autoGenFlag: false,
      parentId: "",
      // 表单校验
      rules: {
        supplyCode: [
          { required: true, message: "补料编码不能为空", trigger: "blur" }
        ],
        workorderCode: [
          { required: true, message: "生产工单不能为空", trigger: "blur" }
        ],
        bomItemCode: [
          { required: true, message: "BOM版本不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "补料原因不能为空", trigger: "blur" }
        ],
        supplyTime: [
          { required: true, message: "补料时间不能为空", trigger: "blur" }
        ]
      },
      form: {
        id: null,
        supplyCode: null,
        workorderId: null,
        workorderCode: null,
        checkUser: null,
        supplyTime: null,
        checkTime: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
      }
    }
  },
  methods: {
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("MATERIAL_SUPPLY").then(response => {
          this.form.supplyCode = response;
        });
      } else {
        this.form.supplyCode = null;
      }
    },
    onSelected(selectedRows) {
      this.selectedRows = selectedRows;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplyCode: null,
        workorderId: null,
        workorderCode: null,
        checkUser: null,
        supplyTime: null,
        checkTime: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        processInstanceId: null,
        warehouseId: null
      };
      this.resetForm("form");
      this.autoGenFlag = false;
      this.personList = [];
      this.activeStep = null;
      this.stepResult = null;
      this.parentId = "";
      this.selectedRows = [];
      console.log("cancel")
    },
    /** 提交按钮 */
    submitForm() {
      console.info("this.selectedRows===>" + JSON.stringify(this.selectedRows));
      if (this.selectedRows == null || this.selectedRows.size == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!"
        });
        return;
      }
      console.info("this.selectedRows===>", JSON.stringify(this.selectedRows));
      this.$refs["form"].validate(valid => {
        if (valid) {
          let reqParam = {
            baseInfo: this.form,
            detailList: this.selectedRows
          };
          if (this.optType !== "check") {
            if (this.form.id != undefined) {
              updateSupplyBatchInfo(reqParam).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              //新增
              addSupplyBatchInfo(reqParam).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    init(data) {
      this.open = true;
      debugger
      console.log("data", data)
      Object.assign(this.form, data);
      this.$nextTick(() => {
        this.$refs.supplyDetail.init(this.form, this.optType);
      });
    }
  }
}
</script>